<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Paginator;


class Index extends Controller
{
    //修改密码
    public function editPassword()
    {
        if(request()->isPost())
        {
        $name=session("findPasswordByEmail_name");
        $password=think_weiphp_md5(input('post.newPass'));
        $re=Db::name('users')->where("username='$name'")->setField('userpass',$password);
        if ($re) {
           $this->success('密码修改成功喽，回去登陆一波吧','Index/index');
           session("findPasswordByEmail_name",null);
        }
        else{
            $this->error("失败啦");
        } 
        }
       return $this->fetch();
    }

    //验证邮箱验证码正确性
    public function checkCode()
    {
        $code=input('post.checkCode');
        $name=session('findPasswordByEmail_name');
        $rand=Db::name('users')->where("username like '%$name%'")->column('rand');
        if ($code==$rand[0]) {
           $this->success('马上让你改密码','Index/editPassword');
        }
        return $this->fetch();

    }
    //邮箱找回密码
     public function findPasswordByEmail()
    {
        /**/
        if (request()->isPost()) {
        /* $mail="1401734643@qq.com";*/
        $title="找回密码";
        $name=input('request.name');
        $mail=input('request.email');
         $username=Db::name('users')->where("username like '%$name%'")->column('username');
         if($username==null)
         {
            $this->error("用户名不存在");
            return;
         }
         session("findPasswordByEmail_name",$name);
         $email=Db::name('users')->where("username='$name'")->column('email');
         if ($mail!=$email[0]) {
            $this->error("邮箱不存在，再想想啊");
         }
        
            $rand=rand(11111,99999);
            Db::name('users')->where("username='$name' AND email='$mail'")->setField('rand',$rand);
            $content="您的验证码：".$rand;
            if (sendMail($mail,$title,$content)) {
                $this->success("发送成功",'index/checkCode');
            }
            else{
                $this->error("发送失败");
            }
        }
        return $this->fetch();
            
    }
	//显示首页
    public function index()
    {
        //显示首页
     //轮播图显示
        $res= \think\Db::view('slideshow','shop_id,up,createtime,picture')
        ->view('dining','shopname','slideshow.shop_id = dining.shop_id')
            ->order('createtime desc')
            ->select();
        $this->assign('slideshowres',$res);
        //新进餐厅显示
        $newshop = \think\Db::name('dining')->order('createtime desc')
        ->where('state',1)
        ->paginate(3);
        $this->assign('newsshops',$newshop);
     /*新进菜品显示*/
        $newsfood =\think\Db::name('food')->order('addate desc')->paginate(3);
        $this->assign('newsfoods',$newsfood);
        $offer = \think\Db::name('food')->order('startdate desc')->paginate(3);
        $this->assign('specialoffer',$offer);
         /*
        根据店铺的销量和对热门餐厅进行排序
        */
        $hotshop = \think\Db::name('dining')
        ->alias('a')
        ->join('__ORDER__ b','a.shop_id=b.shop_id')
        ->field('a.shop_id,shopname,pic,level,fei,obserave,sum(b.num)')
        ->group('a.shop_id')
        ->order('sum(b.num) desc')
        ->paginate(4);
    $this->assign('hotshop', $hotshop);
    $hotfood = \think\Db::name('food')
    ->alias('a')
    ->join('__ORDER__ b','a.food_id=b.food_id')
    ->join('__DINING__ c','a.shop_id=c.shop_id')
    ->field('a.shop_id,foodname,a.pic,price,obserave,sum')
    ->order('sum desc')
    ->paginate(6);
    // dump($hotfood);
    // die();
    $this->assign('hotfood',$hotfood);

        return $this->fetch();
    }
    //显示查询结果
    public function search()
   {
    $select = input('select');
    $searc = input('Search');
           if ($select ==0) {
            //0代表店铺
              
                $searchlists = \think\Db::name('dining')->where("shopname like '%$searc%'")->paginate(2);
                $this->assign('searchlists',$searchlists);
                 $this->assign('page',$searchlists->render());
                return $this->fetch('searchlists');
            }
            elseif ($select==1) { 
                //1代表美食
              $map['foodname'] = array('like','%'.$searc.'%');
              $searchlists = \think\Db::name('food')->where($map)->paginate(6);
                    $this->assign('searchlists',$searchlists); 
                    return $this->fetch('searchFood');
                
            }
    }
       

}
